package com.cskaoyan.controller.wxcontroller;


import com.cskaoyan.bean.pojo.Groupon;
import com.cskaoyan.bean.pojo.Order;
import com.cskaoyan.bean.pojo.OrderGoods;
import com.cskaoyan.bean.pojo.User;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.wx.WxBaseRespVo;
import com.cskaoyan.bean.vo.wx.WxGrouponDetailVo;
import com.cskaoyan.bean.vo.wx.WxGrouponMyVo;
import com.cskaoyan.bean.vo.wx.groupondetail.*;
import com.cskaoyan.service.GoodsService;
import com.cskaoyan.service.OrderService;
import com.cskaoyan.service.PromotionService;
import com.cskaoyan.service.UserService;
import com.cskaoyan.utils.OrderStateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author: lsy
 * @class: GrouponController
 */
@RestController("wxGrouponController")
@RequestMapping("/wx/groupon")
public class GrouponController {

    @Autowired
    PromotionService promotionService;

    @Autowired
    GoodsService goodsService;

    @Autowired
    OrderService orderService;

    @Autowired
    UserService userService;

    /**
     * @author: lsy
     * @title: my
     * @description:
     * showType == 0, 从groupon找create_user_id
     * showType == 1, 从groupon找user_id
     * @param: [map]
     * @return: com.cskaoyan.bean.vo.WxBaseRespVo
     */
    @GetMapping("my")
    public BaseRespVo my(int showType) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.fail("请登录");
        }
        WxBaseRespVo<WxGrouponMyVo> wxBaseRespVo = promotionService.queryGrouponByUserIdAndShowType(showType, user.getId());
        return BaseRespVo.ok(wxBaseRespVo);
    }

    @RequestMapping("list")
    public BaseRespVo list() {
        return BaseRespVo.ok();
    }

    @RequestMapping("query")
    public BaseRespVo query() {
        return BaseRespVo.ok();
    }

    /**
     * @author: lsy
     * @title: detail
     * @description: 根据团购id获取该用户该团购内容详情
     * @param: [grouponId]
     * @return: com.cskaoyan.bean.vo.BaseRespVo
     */
    @GetMapping("detail")
    public BaseRespVo detail(int grouponId) {
        // 注意传来的是表的主键id 而不是groupon_id
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.fail("请登录");
        }
        WxGrouponDetailVo wxGrouponDetail = new WxGrouponDetailVo();
        Groupon groupon = promotionService.queryGrouponById(grouponId);

        List<OrderGoods> orderGoodsList = goodsService.queryOrderGoodsByOrderId(groupon.getOrderId());
        Order order = goodsService.queryOrderByOrderId(groupon.getOrderId());

        List<Groupon> sameGrouponList = promotionService.queryGrouponByGrouponId(groupon.getGrouponId());
        ArrayList<WxJoinerVo> wxJoinerVos = new ArrayList<>();
        for (Groupon groupon1 : sameGrouponList) {
            Integer userId = groupon1.getUserId();
            User user1 = userService.queryUserById(userId);

            WxJoinerVo wxJoinerVo = new WxJoinerVo();
            wxJoinerVo.setNickname(user1.getNickname());
            wxJoinerVo.setAvatar(user1.getAvatar());
            wxJoinerVo.setId(userId);
            wxJoinerVos.add(wxJoinerVo);
        }
        wxGrouponDetail.setJoiners(wxJoinerVos);

        wxGrouponDetail.setGroupon(groupon);
        wxGrouponDetail.setId(groupon.getId());

        // todo 暂时写0 前端也不显示，无所谓了
        wxGrouponDetail.setLinkGrouponId(0);

        ArrayList<WxOrderGoodsVo> wxOrderGoodsVos = new ArrayList<>();
        for (OrderGoods orderGoods : orderGoodsList) {
            WxOrderGoodsVo wxOrderGoodsVo = new WxOrderGoodsVo();
            wxOrderGoodsVo.setPicUrl(orderGoods.getPicUrl());
            wxOrderGoodsVo.setGoodsName(orderGoods.getGoodsName());
            wxOrderGoodsVo.setNumber(orderGoods.getNumber());
            String[] specifications = orderGoods.getSpecifications();
            LinkedList<String> specificationList = new LinkedList<>();
            for (String specification : specifications) {
                specificationList.add(specification);
            }
            wxOrderGoodsVo.setGoodsSpecificationValues(specificationList);
            wxOrderGoodsVo.setRetailPrice(orderGoods.getPrice());
            wxOrderGoodsVos.add(wxOrderGoodsVo);
        }
        wxGrouponDetail.setOrderGoods(wxOrderGoodsVos);

        wxGrouponDetail.setOrderId(groupon.getOrderId());

        WxOrderInfoVo wxOrderInfoVo = new WxOrderInfoVo();
        wxOrderInfoVo.setId(order.getId());
        WxHandleOptionVo handleOption = new WxHandleOptionVo();
        // 用户确认或者系统确认
        if (order.getOrderStatus() == 402 || order.getOrderStatus() == 401) {
            handleOption.setConfirm(true);
        } else {
            handleOption.setConfirm(false);
        }
        wxOrderInfoVo.setHandleOption(handleOption);
        wxOrderInfoVo.setAddTime(order.getAddTime());
        wxOrderInfoVo.setOrderSn(order.getOrderSn());
        wxOrderInfoVo.setActualPrice(order.getActualPrice());
        wxOrderInfoVo.setOrderStatusText(OrderStateUtil.getStatusText(order.getOrderStatus()));
        wxOrderInfoVo.setConsignee(order.getConsignee());
        wxOrderInfoVo.setMobile(order.getMobile());
        wxOrderInfoVo.setAddress(order.getAddress());
        wxOrderInfoVo.setGoodsPrice(order.getGoodsPrice());
        wxOrderInfoVo.setFreightPrice(order.getFreightPrice());
        // expCode设置为订单id
        wxOrderInfoVo.setExpCode(order.getId());
        // todo expNo不知道啥意思，先随便传一下
        wxOrderInfoVo.setExpNo(null);
        wxGrouponDetail.setOrderInfo(wxOrderInfoVo);

        WxTraceVo wxTraceVo = new WxTraceVo();
        if (order.getOrderStatus() == 401) {
            wxTraceVo.setAcceptStation("用户确认收货");
        } else if (order.getOrderStatus() == 402) {
            wxTraceVo.setAcceptStation("系统确认收货");
        } else {
            wxTraceVo.setAcceptStation("未收货");
        }
        wxTraceVo.setAcceptTime(order.getConfirmTime());
        ArrayList<WxTraceVo> wxTraceVos = new ArrayList<>();
        wxTraceVos.add(wxTraceVo);
        wxGrouponDetail.setTraces(wxTraceVos);

        wxGrouponDetail.setFlag(true);

        wxGrouponDetail.setShipperName(order.getShipChannel());
        wxGrouponDetail.setLogisticCode(order.getShipSn());

        return BaseRespVo.ok(wxGrouponDetail);
    }

    @RequestMapping("join")
    public BaseRespVo join() {
        return BaseRespVo.ok();
    }
}
